package com.itheima.reggie.config;

import com.itheima.reggie.common.JacksonObjectMapper;
import com.itheima.reggie.common.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration //表示该类是一个配置类
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)//添加拦截器
                .addPathPatterns("/**") //拦截的路径
                .excludePathPatterns("/user/login") //移动端登录
                .excludePathPatterns("/user/sendMsg") //移动端验证码
                .excludePathPatterns("/employee/login") //排除的路径
                .excludePathPatterns("/backend/**") //排除的路径，浏览器访问路径以/backend开头的路径  /backend/a/b/c/d
                .excludePathPatterns("/front/**"); //排除的路径
    }

    //扩展MVC的消息转换器
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //1. 创建一个消息转换器对象 MappingJackson2HttpMessageConverter
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //TODO 最核心的操作，设置核心对象。（不设置，默认是 ObjectMapper）
        messageConverter.setObjectMapper(new JacksonObjectMapper());

        //2. 将消息转换器对象添加到集合中（注意：需要放在集合的最前面，优先级高一些）
        converters.add(0, messageConverter);
    }

}
